{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fantastic-architecture",
   "metadata": {},
   "source": [
    "# Triton Testing\n",
    "\n",
    "## Local Setup\n",
    "\n",
    "\n",
    "* `make start-scheduler` \n",
    "* `make start-envoy`\n",
    "* `make start-triton-agent`\n",
    "* `make start-triton`\n",
    "* `make start-rclone`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "french-mistress",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model\":{ \\\n",
    "              \"meta\":{\"name\":\"tfsimple\"},\\\n",
    "              \"modelSpec\":{\"uri\":\"gs://seldon-models/triton/simple\",\\\n",
    "                           \"requirements\":[\"tensorflow\"],\\\n",
    "                           \"memoryBytes\":500},\\\n",
    "              \"deploymentSpec\":{\"replicas\":1}}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  0.0.0.0:9004 seldon.mlops.scheduler.Scheduler/LoadModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acting-nickname",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model\":{\"name\":\"tfsimple\"}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  0.0.0.0:9004 seldon.mlops.scheduler.Scheduler/ModelStatus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "brazilian-reception",
   "metadata": {},
   "outputs": [],
   "source": [
    "!curl -v http://0.0.0.0:9000/v2/models/tfsimple/infer -H \"Content-Type: application/json\" -H \"seldon-model: tfsimple\"\\\n",
    "        -d '{\"inputs\":[{\"name\":\"INPUT0\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "entire-harbor",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model_name\":\"tfsimple\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' \\\n",
    "        -plaintext \\\n",
    "        -import-path ../../apis \\\n",
    "        -proto ../../apis/mlops/v2_dataplane/v2_dataplane.proto \\\n",
    "        -rpc-header seldon-model:iris \\\n",
    "        0.0.0.0:9000 inference.GRPCInferenceService/ModelInfer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "falling-contact",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model\":{\"name\":\"tfsimple\"}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  0.0.0.0:9004 seldon.mlops.scheduler.Scheduler/UnloadModel"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "patient-denmark",
   "metadata": {},
   "source": [
    "#  K8S Test\n",
    "\n",
    "Assumes you have a Kind cluster running with metallb. No other dependencies required.\n",
    "You can use [Seldon Ansible Kind Playbook](https://github.com/SeldonIO/ansible-k8s-collection/blob/master/playbooks/kind.yaml)\n",
    "\n",
    "## Setup\n",
    "\n",
    "* `make kind-image-install-all`\n",
    "* `make deploy` \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "instructional-border",
   "metadata": {},
   "outputs": [],
   "source": [
    "SCHEDULER_IP=!kubectl get svc seldon-scheduler -n seldon-mesh -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "SCHEDULER_IP=SCHEDULER_IP[0]\n",
    "import os\n",
    "os.environ['SCHEDULER_IP'] = SCHEDULER_IP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "mathematical-gossip",
   "metadata": {},
   "outputs": [],
   "source": [
    "MESH_IP=!kubectl get svc seldon-mesh -n seldon-mesh -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "MESH_IP=MESH_IP[0]\n",
    "import os\n",
    "os.environ['MESH_IP'] = MESH_IP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "driving-cache",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model\":{ \\\n",
    "              \"meta\":{\"name\":\"tfsimple\"},\\\n",
    "              \"modelSpec\":{\"uri\":\"gs://seldon-models/triton/simple\",\\\n",
    "                           \"requirements\":[\"tensorflow\"],\\\n",
    "                           \"memoryBytes\":500},\\\n",
    "              \"deploymentSpec\":{\"replicas\":1}}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/LoadModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lesbian-thursday",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model\":{\"name\":\"tfsimple\"}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/ModelStatus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "transparent-restoration",
   "metadata": {},
   "outputs": [],
   "source": [
    "!curl -v http://${MESH_IP}/v2/models/tfsimple/infer -H \"Content-Type: application/json\" -H \"seldon-model: tfsimple\"\\\n",
    "        -d '{\"inputs\":[{\"name\":\"INPUT0\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"data\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],\"datatype\":\"INT32\",\"shape\":[1,16]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "unnecessary-marina",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model_name\":\"tfsimple\",\"inputs\":[{\"name\":\"INPUT0\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]},{\"name\":\"INPUT1\",\"contents\":{\"int_contents\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]},\"datatype\":\"INT32\",\"shape\":[1,16]}]}' \\\n",
    "        -plaintext \\\n",
    "        -import-path ../../apis \\\n",
    "        -proto ../../apis/mlops/v2_dataplane/v2_dataplane.proto \\\n",
    "        -rpc-header seldon-model:tfsimple \\\n",
    "        ${MESH_IP}:80 inference.GRPCInferenceService/ModelInfer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "artificial-brand",
   "metadata": {},
   "outputs": [],
   "source": [
    "!grpcurl -d '{\"model\":{\"name\":\"tfsimple\"}}' \\\n",
    "         -plaintext \\\n",
    "         -import-path ../../apis \\\n",
    "         -proto ../../apis/mlops/scheduler/scheduler.proto  ${SCHEDULER_IP}:9004 seldon.mlops.scheduler.Scheduler/UnloadModel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "horizontal-grammar",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
